﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class backstage_article : System.Web.UI.Page
{
    // Fields
    protected Article ac = new Article();
    protected ArticleManager am = new ArticleManager();
    protected ArticleTypeManager atm = new ArticleTypeManager();

    protected string atId = "00000000-0000-0000-0000-000000000000";

    

    // Methods
    private void ArticleBind()
    {
        try
        {
            DataTable articleById = this.am.GetArticleById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (articleById.Rows.Count > 0)
            {
                this.txtArticleTilte.Value = articleById.Rows[0]["articleTilte"].ToString();
                base.ClientScript.RegisterStartupScript(base.GetType(), "st", "<script type=\"text/javascript\">selectIsExitItem(\"selectType\",\"" + articleById.Rows[0]["articleType"].ToString() + "\");</script>");
                this.txtAuthor.Value = articleById.Rows[0]["author"].ToString();
                if (Convert.ToInt32(articleById.Rows[0]["isShow"]) == 1)
                {
                    this.rdIsShow1.Checked = true;
                }
                else
                {
                    this.rdIsShow0.Checked = true;
                }
                this.fckArticleContent.Value = articleById.Rows[0]["articleContent"].ToString();
                this.txtKeywords.Value = articleById.Rows[0]["keywords"].ToString();
                this.txtaDescription.Value = articleById.Rows[0]["description"].ToString();
            }
        }
        catch
        {
        }
    }

    protected StringBuilder ArticleTypeBind()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            this.RecursionArticeType(sb, "00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString());
        }
        catch
        {
        }
        return sb;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.ac.ArticleTilte = this.txtArticleTilte.Value;
            this.ac.TypeIdentity = Convert.ToInt32(base.Request.Form["articleType"]);
            this.ac.Author = this.txtAuthor.Value;
            this.ac.IsShow = Convert.ToInt32(base.Request.Form["isShow"]);
            this.ac.ArticleContent = this.fckArticleContent.Value;
            this.ac.Keywords = this.txtKeywords.Value;
            this.ac.Description = this.txtaDescription.Value;
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.ac.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.am.ModifyArticle(this.ac))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"article_list.aspx?atId=" + base.Request.QueryString["atId"] + "\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else
            {
                this.ac.LanguageVersion = this.Session["language_version"].ToString();
                if (this.am.AddArticle(this.ac))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"article_list.aspx?atId=" + base.Request.QueryString["atId"] + "\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
                }
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑文章");
                return builder;
            }
            builder.Append("添加文章");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["atId"] != null)))
        {
            this.atId = base.Request.QueryString["atId"];
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.ArticleBind();
            }
            else
            {
                this.rdIsShow1.Checked = true;
            }
        }
    }

    private void RecursionArticeType(StringBuilder sb, string parentType, string languageVersion)
    {
        DataTable articleTypeByParentTypeAndLanguageVersion = this.atm.GetArticleTypeByParentTypeAndLanguageVersion(parentType, languageVersion);
        if (articleTypeByParentTypeAndLanguageVersion.Rows.Count > 0)
        {
            foreach (DataRow row in articleTypeByParentTypeAndLanguageVersion.Rows)
            {
                string str = "";
                int num = Convert.ToInt32(row["depth"]);
                if (num > 0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        str = str + "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    str = str + "┣";
                }
                sb.AppendLine("<option value=\"" + row["typeIdentity"].ToString() + "\"  " + ((base.Request.QueryString["atId"] == row["id"].ToString().ToUpper()) ? "selected='selected'" : "") + ">" + str + row["typeName"].ToString() + "</option>");
                this.RecursionArticeType(sb, row["id"].ToString(), languageVersion);
            }
        }
    }

}